<?php namespace App\Services;

use Auth;
use Request;
use Validator;

class Tools
{
    /**
     * 验证是否为合法的 Email 地址
     *
     * @param $str
     * @return bool
     */
    public static function isEmail($str)
    {
        return Validator::make(['email' => $str], ['email' => 'required|email'])->passes();
    }

    /**
     * 验证是否为合法的手机号码
     *
     * @param $str
     * @return bool
     */
    public static function isPhone($str)
    {
        return Validator::make(['phone' => $str], ['phone' => ['required', 'regex:/1\d{10}/']])->passes();
    }

    public static function buildApiResponse($message = '', $data = [], $redirect = '', $level = 'success', $code = 0, $httpStatusCode = 200)
    {
        if (Request::ajax() || Request::wantsJson()) {
            return response()->json([
                'code'     => $code,
                'level'    => $level,
                'message'  => $message,
                'data'     => $data,
                'redirect' => $redirect,
            ], $httpStatusCode);
        } else {
            return view('api/redirect', [
                'code'     => $code,
                'level'    => $level,
                'message'  => $message,
                'data'     => $data,
                'redirect' => $redirect,
            ]);
        }
    }

}